package com.vichat.common.service.impl;

import com.vichat.common.cache.ApplicationCache;
import com.vichat.common.context.ContextHolder;
import com.vichat.common.service.IExcelParser;
import com.vichat.common.util.ExcelUtil;
import com.vichat.common.vo.excel.ErrorRow;
import com.vichat.common.vo.excel.ExcelDataWrapper;
import com.vichat.common.vo.excel.RowWrapper;
import org.apache.log4j.Logger;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;


public abstract class AbstractExcelParser implements IExcelParser {

	public final Logger logger = Logger.getLogger(AbstractExcelParser.class);
	public static final String EXCEL_DATA_CACHE_KEY = "_excel_cache";
	public static final int MAX_EXCEL_ROW = 65536;



	public Workbook createWorkbook(InputStream in) throws IOException, InvalidFormatException {
		return ExcelUtil.createWorkbook(in);
	}

	public String getExcelCacheKey(String unionKey) {
		return unionKey + AbstractExcelParser.EXCEL_DATA_CACHE_KEY;
	}

	public void put2Cache(ExcelDataWrapper excelDataWrapper, String unionKey) {
		ApplicationCache cache = (ApplicationCache) ContextHolder.getBean(ApplicationCache.BEAN_ID);
		cache.putTemporaryCacheObject(getExcelCacheKey(unionKey), excelDataWrapper);
	}

	public ExcelDataWrapper getExcelDataWrapperCache(String unionKey) {
		ApplicationCache cache = (ApplicationCache) ContextHolder.getBean(ApplicationCache.BEAN_ID);
		return (ExcelDataWrapper) cache.getTemporaryCacheObject(getExcelCacheKey(unionKey));
	}

	public void clearExcelDataWrapperCache(String unionKey) {
		ApplicationCache cache = (ApplicationCache) ContextHolder.getBean(ApplicationCache.BEAN_ID);
		cache.removeTemporaryCacheObject(getExcelCacheKey(unionKey));
	}

	protected boolean isNotNullCell(Cell cell) {
		return ExcelUtil.isNotNullCell(cell);
	}



	public List getEntityList(String unionKey) {
		ExcelDataWrapper excelDataWrapper = this.getExcelDataWrapperCache(unionKey);
		Collection<RowWrapper> rowWrappers = excelDataWrapper.getRowWrappers();

		List dataList = new ArrayList();
		for (RowWrapper rowWrapper : rowWrappers) {
			if (rowWrapper.isValidate()) {
				dataList.add(rowWrapper.getEntity());
			}
		}
		return dataList;
	}

	public List getErrorList(String unionKey) {
		ExcelDataWrapper excelDataWrapper = this.getExcelDataWrapperCache(unionKey);
		Collection<RowWrapper> rowWrappers = excelDataWrapper.getRowWrappers();
		List errList = new ArrayList();
		for (RowWrapper rowWrapper : rowWrappers) {
			if (!rowWrapper.isValidate()) {
				errList.add(new ErrorRow(rowWrapper.getRow(), rowWrapper.getErrorMsg()));
			}
		}
		return errList;
	}

//    public final static String DATE_OUTPUT_PATTERNS = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
//
//    public String getCellValue(Cell cell) {
//        String ret;
//        switch (cell.getCellType()) {
//            case Cell.CELL_TYPE_BLANK:
//                ret = "";
//                break;
//            case Cell.CELL_TYPE_BOOLEAN:
//                ret = String.valueOf(cell.getBooleanCellValue());
//                break;
//            case Cell.CELL_TYPE_ERROR:
//                ret = null;
//                break;
//            case Cell.CELL_TYPE_FORMULA:
//                Workbook wb = cell.getSheet().getWorkbook();
//                CreationHelper crateHelper = wb.getCreationHelper();
//                FormulaEvaluator evaluator = crateHelper.createFormulaEvaluator();
//                ret = getCellValue(evaluator.evaluateInCell(cell));
//                break;
//            case Cell.CELL_TYPE_NUMERIC:
//                if (DateUtil.isCellDateFormatted(cell)) {
//                    Date theDate = cell.getDateCellValue();
//                    ret = simpleDateFormat.format(theDate);
//                } else {
//                    ret = NumberToTextConverter.toText(cell.getNumericCellValue());
//                }
//                break;
//            case Cell.CELL_TYPE_STRING:
//                ret = cell.getRichStringCellValue().getString();
//                break;
//            default:
//                ret = null;
//        }
//
//        return ret; //有必要自行trim
//    }

}
